package com.hua.yuan.live.repertory.cloud_data.loader;

import android.support.annotation.NonNull;
import android.support.v4.app.LoaderManager;
import android.text.TextUtils;
import com.alibaba.fastjson.JSONObject;
import com.hua.yuan.common.https.OkHttpModule;
import com.hua.yuan.live.repertory.cloud_data.JsonResult;
import com.hua.yuan.live.repertory.cloud_data.api.LivePlayApi;
import com.hua.yuan.live.repertory.cloud_data.res.LivePlayBean;
import com.hua.yuan.live.repertory.cloud_data.res.LivePlayComband;
import com.hua.yuan.live.repertory.cloud_data.res.ResultBean;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 直播入口
 */
public class LiveRoomLoader {

    private LivePlayApi mApi;

    private LoaderManager.LoaderCallbacks<LivePlayBean> mCallback;

    private Disposable mPublish;

    public LiveRoomLoader() {
        mApi = OkHttpModule.getInstance().getRetrofit().create(LivePlayApi.class);
    }

    /**
     * 获取主播的接口数据
     *
     * @param pToken    用户登录的token
     * @param pAnchorId 主播的Id
     */
    public void requestRoomLife(@NonNull final String pToken, @NonNull final int pAnchorId,
                                @NonNull final LoaderManager.LoaderCallbacks<LivePlayComband> pCallback) {

        if (mPublish != null) {
            mPublish.dispose();
        }
        mPublish = Observable.create(new ObservableOnSubscribe<LivePlayComband>() {

            @Override
            public void subscribe(ObservableEmitter<LivePlayComband> pEmitter) throws Exception {

                Call<String> call = mApi.getUserMoney(pToken);
                try {
                    Response<String> response = call.execute();
                    if (TextUtils.isEmpty(response.body())) {
                        pEmitter.onError(null);
                    }
                    ResultBean resultData = JSONObject.parseObject(response.body(), ResultBean.class);
                    LivePlayComband comband = new LivePlayComband(resultData);
                    pEmitter.onNext(comband);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).subscribeOn(Schedulers.newThread())
                .map(new Function<LivePlayComband, LivePlayComband>() {
                    @Override
                    public LivePlayComband apply(LivePlayComband comband) throws Exception {

                        //执行数据获取
                        Call<String> call = mApi.getPlayUrl(pToken, pAnchorId);
                        try {
                            Response<String> response = call.execute();
                            if (TextUtils.isEmpty(response.body())) {
                                return null;
                            }
                            JsonResult<JSONObject> resultData = JSONObject.parseObject(response.body(), JsonResult.class);
                            if (resultData == null) {
                                return null;
                            }
                            JSONObject data = resultData.getData();
                            if (data == null) {
                                return null;
                            }
                            LivePlayBean targetData = JSONObject.parseObject(data.toJSONString(), LivePlayBean.class);
                            if (targetData == null) {
                                return null;
                            }
                            comband.setLivePlayBean(targetData);
                            return comband;
                        } catch (IOException e) {
                            e.printStackTrace();

                        }
                        return null;
                    }

                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<LivePlayComband>() {
                    @Override
                    public void accept(LivePlayComband playBean) throws Exception {
                        pCallback.onLoadFinished(null, playBean);
                        mPublish.dispose();
                    }
                });
    }



}
